//
//  LDJobfinderDetailBaseVCTableViewController.swift
//  DingDongZP
//
//  Created by 三秒文化 on 16/6/6.
//  Copyright © 2016年 iyhjiang.com. All rights reserved.
//

import UIKit
import YYModel
import BlocksKit
import SwiftyJSON


class LDJobfinderDetailBaseVC: LDBaseCustomizeVC {
    
    //
    enum jobFinderDetailType:Int {
        case recommend = 11
        case nofi = 12
    }
    
    //通知详情 模型暂时没有统一
    var modelType:jobFinderDetailType?
    var resumeDict = [String:AnyObject]()
    //首页推荐入口
    var model:DDJobfinderDetail? {
        
        didSet{
            
            modelType = .recommend
            self.title = model?.name
            let userid = model?.userid ?? "0"
            sendRequest(userid)
        }
    }
    //通知详情入口
    var notiModel:DDEmployerNotification? {
    
        didSet{
           
            modelType = .nofi
            self.title = notiModel?.name
            let jobFinderID = notiModel?.jobfinderid ?? "0"
            sendRequest(jobFinderID)
        }
    }
    
    var BtnH:CGFloat = 60
    var tableView:UITableView?
    
    var type:MyResumeType?
    //cell数据,为统一返回的数据
    var model0 = LDBossFinderResumeNameCellModel()
    var model1 = LDBossFinderResumeHightLightCellModel()
    var model2 = LDBossFinderResumeExpCellModel()
    var model3 = LDBossFinderResumeEduCellModel()
    
    //btn
    var btn1:UIButton!
    var btn2:UIButton!
    
    override func viewDidLoad() {
        
        super.viewDidLoad()
        setupTableView()
        setupNav()
        setupBtn()
    }
 
    override func viewWillAppear(animated: Bool) {
        
        rdv_tabBarController.setTabBarHidden(true, animated: true)
    }
    
    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //网络请求
    func sendRequest(userid:String){
        
        //请求微简历信息
        let intid:Int = Int(userid) ?? 0
        LDBossInfoNet.sharedInstance.queryJobFinderMyResume(intid, success: {
            
            [weak self] (dict) in
            
            NSOperationQueue.mainQueue().addOperationWithBlock({ [weak self] in
                
                var isaudio = ""
                
                if self?.modelType == .recommend {
                   
                    isaudio = self?.model?.isresumeaudio ?? "0"
                } else {
                    
                    isaudio = self?.notiModel?.isresumeaudio ?? "0"
                }
                
                
                self?.type = isaudio == "1" ? .Voice:.Text
                self?.resumeDict = dict
                self?.tableView?.reloadData()
            })
            
            }, failure: nil)
        
        //请求是关注
        
        DDJobfindersService.shareInstance().queryJobfindersInterestingStatusWithJobfindeId(userid, successBlock: {
            [weak self] (data) in
            
            let dict = data as? NSDictionary
            let isInterest = dict!["data"] as? Int
            
            if isInterest == 1 {
                
                self?.btn1.selected = true
            } else {
                
                self?.btn1.selected = false
            }
            
            }, failBlock: nil)
    }
    
    
    func setupTableView() {
        
        view.backgroundColor = LDGolbalColor.White
        tableView = LDBaseTableView.init(frame: LDFrame.ScreenBounds, style: UITableViewStyle.Plain)
        tableView?.delegate = self
        tableView?.dataSource = self
        view.addSubview(tableView!)
        
        tableView?.translatesAutoresizingMaskIntoConstraints = false
        tableView?.contentInset = UIEdgeInsetsMake(7, 0, BtnH + LDFrame.NavH, 0)
    }
    

//    override func viewWillAppear(animated: Bool) {
//        
////        tabBarController?.tabBar.hidden = true
//    }
    
    func setupNav() {
        
        let rightview = UIView.init(frame: CGRectMake(0, 0, 64, 44))
        
        let repbtn = UIButton.setDetailsBtn(nil, picName: "common_nav_report", btnBackColor: nil, titleColor: nil)
        repbtn?.frame = CGRectMake(32, 1, 42, 42)
//        let shareBtn = UIButton.setDetailsBtn(nil, picName: "common_nav_share", btnBackColor: nil, titleColor: nil)
        
        rightview.addSubview(repbtn!)
        
        let rightbar = UIBarButtonItem(customView: rightview)
        navigationItem.rightBarButtonItem = rightbar
        
        //举报
        repbtn?.rac_signalForControlEvents(UIControlEvents.TouchUpInside).subscribeNext({ [weak self](obj) in
            
            LDCheckPublicInfo.shareInstance.checkoutReportJobfinderList({ (data) in
                
                var titltArr = [String]()
                var titleArrID = [String]()
                
                for item in data {
                    
                    titltArr.append(item["complaint_text"] as? String ?? LDNull)
                    titleArrID.append(item["complaint_id"] as? String ?? LDNull)
                }
                
                
                //显示action
                self?.showLDActionSheetView("举报牛人", labHeight: nil, labsTitleArr: titltArr, selClosure: { (index) in
                    
                    let params = ["jobfinder_id":self?.model?.userid ?? "0","complaint_id":titleArrID[index]]
                    
                    //
                    
                    LDBossInfoNet.sharedInstance.reportJobfinder(params, success: {
                       
                        LDShowHUB.shareInstance.showSucc("举报成功")
                        
                    }, failure: { (error) in
                        
                        LDShowHUB.shareInstance.showToastNoti("举报失败")
                    })
                })
            })
        })
    }
}

extension LDJobfinderDetailBaseVC:UITableViewDelegate,UITableViewDataSource {
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        if type == .Text {
            
            return 4
            
        } else if type == .Voice {
            
            return 2
            
        } else {
            
            return 0
        }
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        
        switch indexPath.row {
            
        case 0:
            
            var cell = tableView.dequeueReusableCellWithIdentifier("cells0") as? LDBossFinderResumeNameCell
            if cell == nil {
                cell = LDBossFinderResumeNameCell.init(style: UITableViewCellStyle.Default, reuseIdentifier: "cellls0")
            }
            cell!.models = model0
            return cell!
            
        case 1:
            
            if type == .Text {
            
                var cell = tableView.dequeueReusableCellWithIdentifier("cells1") as? LDBossFinderResumeHightLightCell
                if cell == nil {
                    cell = LDBossFinderResumeHightLightCell.init(style: UITableViewCellStyle.Default, reuseIdentifier: "cellls1")
                }
                cell?.model = model1
                return cell!
            } else {

                let cell = LDFinderDetailVoiceCell.init(style: .Default, reuseIdentifier: "cellls1")
                cell.resumeDict = resumeDict
                return cell
            }
            
            
        case 2:
            
            var cell = tableView.dequeueReusableCellWithIdentifier("cells2") as? LDBossFinderResumeExpCell
            if cell == nil {
                cell = LDBossFinderResumeExpCell.init(style: UITableViewCellStyle.Default, reuseIdentifier: "cellls2")
            }
            cell!.model = model2
            return cell!
            
        case 3:
            
            var cell = tableView.dequeueReusableCellWithIdentifier("cells3") as? LDBossFinderResumeEduCell
            if cell == nil {
                cell = LDBossFinderResumeEduCell.init(style: UITableViewCellStyle.Default, reuseIdentifier: "cellls3")
            }
            cell?.model = model3
            return cell!
            
        default:
            
            return UITableViewCell()
        }
        
    }
    
    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        
        var height:CGFloat = 0
        
        switch indexPath.row {
            
        case 0:
            
            model0 = LDBossFinderResumeNameCellModel.yy_modelWithDictionary(resumeDict)!
            height = model0.height
            
        case 1:
            
            if type == .Voice {
                
                height = 330
                
            } else {
                model1 = LDBossFinderResumeHightLightCellModel.yy_modelWithDictionary(resumeDict)!
                height = model1.height
            }
            
            
        case 2:
            
            model2 = LDBossFinderResumeExpCellModel.yy_modelWithDictionary(resumeDict)!
            height = model2.height
            
        case 3:
            
            model3 = LDBossFinderResumeEduCellModel.yy_modelWithDictionary(resumeDict)!
            height = model3.height
            
        default:
            height = 0
        }
        
        return height
    }
}

extension LDJobfinderDetailBaseVC {
    
    //底部编辑按钮
    func setupBtn() {
        
        let divideLine1 = UIView()
        divideLine1.backgroundColor = LDIconColor.LightGray
        
        let divideLine2 = UIView()
        divideLine2.backgroundColor = LDIconColor.LightGray
        
        btn1 = UIButton.setBtn("感兴趣", picName: "interest", btnBackColor: LDFontColor.White, titleColor: LDFontColor.Orange)!
        btn1.setImage(UIImage(named: "interested"), forState: .Selected)
        btn1.titleEdgeInsets = UIEdgeInsetsMake(0, 15, 0, 0)
        btn1.titleLabel?.font = LDFont.Font16
        btn1.layer.cornerRadius = 0
        
        btn1.bk_whenTapped { [weak self] in
            
            DDJobfindersService.shareInstance().markJobfindersInterestingStatusWithJobfindeId(self?.model?.userid, successBlock: { [weak self] (data) in
                
                let dict = data as? NSDictionary
                let isInterest = dict!["data"] as? Int
                
                if isInterest == 1 {
                    LDShowHUB.shareInstance.showToastSucc("关注成功")
                    self?.btn1.selected = true
                } else {
                    LDShowHUB.shareInstance.showToastSucc("取消关注")
                    self?.btn1.selected = false
                }
                
                }, failBlock: nil)
            
        }
        
        btn2 = UIButton.setDetailsBtn("立即沟通", picName: "沟通.png", btnBackColor: LDFontColor.White, titleColor: LDGolbalColor.Green)!
        btn2.titleEdgeInsets = UIEdgeInsetsMake(0, 15, 0, 0)
        btn2.titleLabel?.font = LDFont.Font16
        btn2.layer.cornerRadius = 0
        
        
        btn2.bk_whenTapped { [weak self] in
            if self?.model?.userid == nil {
                LDShowHUB.shareInstance.showToastNoti("用户信息获取失败")
                return
            }

            let vc = DDBossChoosePositionViewController()
            vc.targetID = (self?.model?.userid)!+"_jobfinder"
            vc.titleName = self?.model?.name
            self?.navigationController?.pushViewController(vc, animated: true)
        }
        
        view.addSubview(btn1)
        view.addSubview(btn2)
        view.addSubview(divideLine1)
        view.addSubview(divideLine2)
        
        btn1.snp_makeConstraints { (make) in
            make.left.bottom.equalTo(0)
            make.right.equalTo(self.view.snp_centerX).offset(0)
            make.height.equalTo(BtnH)
        }
        
        btn2.snp_makeConstraints { (make) in
            make.right.bottom.equalTo(0)
            make.left.equalTo(self.view.snp_centerX).offset(0)
            make.height.equalTo(BtnH)
        }
        divideLine1.snp_makeConstraints { (make) in
            make.top.equalTo(btn1)
            make.right.left.equalTo(0)
            make.height.equalTo(0.8)
        }
        divideLine2.snp_makeConstraints { (make) in
            make.centerX.equalTo(view)
            make.centerY.equalTo(btn1)
            
            make.height.equalTo(BtnH - LDMargin_V*2)
            make.width.equalTo(0.8)
        }
    }
    //感兴趣
    func manageExpextJob() {
        
        let vc = LDManageMyResumeVC()
        navigationController?.pushViewController(vc, animated: true)
    }
    //对话
    func editMyResume() {
        
        let vc = editMyResumeVC()
        navigationController?.pushViewController(vc, animated: true)
    }
}